//import store from '../../store'
import  store  from '@/store'
//#ifdef H5
const config = window.appConfig;
//#endif

//#ifndef H5
// 获取第三方平台数据
// const config = uni.getExtConfigSync ? uni.getExtConfigSync() : {};
const config = require('../config');
//#endif
export function request (param) {
  return new Promise((resolve, reject) => {
    param.data = param.data || {};
    /* if (config && config.isTest) {
      param.data['token'] = '8acf8417-8207-4c65-977f-a506b7066eb3';
      param.data['test_open'] = '';
      store.commit('setloginStatus', true);
    } else {
      if (store.state.token) param.data['token'] = store.state.token;
    } */
    let url = '';
    url = config.apiUrl + param.url;
	let token = '';
	token = uni.getStorageSync('token_key');
    uni.request({
      url: url,
      method: param.method,
      dataType: 'json',
      data: param.data,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'verson': '1',
		'token': token // '8acf8417-8207-4c65-977f-a506b7066eb3'
      },
      success (res) {
        const data = res.data;
        if (process.env.NODE_ENV === 'development') {
          //console.log(`${new Date().toLocaleString()}【${param.url} 接口响应】`, res.data)
        }
        if (data.status !== 200 && data.status === 101) {
          //#ifndef APP-PLUS
          uni.reLaunch({
            url: `/packageA/public/tologin`
          });
          //#endif
          // #ifdef APP-PLUS
          if (res.data.msg == '用户token失效') {
            uni.showToast({
              title: '登录已过期，请重新登录',
              icon: 'none',
              duration: 3000
            });
            store.commit('loginOut');
            uni.navigateTo({
              url: `/packageA/public/appLogin`
            })
          } else {
            uni.reLaunch({
              url: `/packageA/public/appLogin`
            })
          }
          // #endif
        } else if (data.status !== 200 && data.status === 102) {
          uni.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 3000
          });
          reject(data);
        } else if (data.status !== 200 && data.status === 112) {
          // 如果是余额不足时，跳转充值
          uni.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500
          });
          setTimeout(() => {
            uni.navigateTo({
              url: `/pages/user/recharge?isOrder=1`
            })
          }, 1500)
          reject(data)
        } else if (data.status !== 200 && data.status === 404) {
          uni.redirectTo({
            url: `/pages/404/404`
          })
        } else {
          resolve(data);
        }
      },
      fail (ret) {
        param.fil && param.fil(ret);
        reject(ret)
      }
    })
  })
}
